package com.it.emall.mapper;

import com.it.emall.domain.Recipes;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *
 * 推荐记录Mapper
 */
@Mapper
public interface RecommendationsMapper {
    /**
     * 查询所有食谱（当用户输入全部 0 时调用）
     */
    // 获取所有食谱列表（用户未登录时）

    @Select("SELECT * FROM recipes")
    List<Recipes> findAllRecipes();

    // 根据用户ID获取用户点赞的食谱ID
    @Select("SELECT recipe_id FROM recommendations WHERE user_id = #{userId}")
    List<Integer> findLikedRecipeIdsByUserId(int userId);

    // 根据用户ID获取用户收藏的食谱ID
    @Select("SELECT recipe_id FROM user_favorites WHERE user_id = #{userId}")
    List<Integer> findFavoritedRecipeIdsByUserId(int userId);

    // 获取所有用户的点赞、收藏信息
    @Select("SELECT DISTINCT user_id FROM recommendations")
    List<Integer> findAllUserIds();

    // 根据食谱ID列表获取推荐的食谱详情
    @Select({
            "<script>",
            "SELECT * FROM recipes WHERE",
            "<if test='list != null and list.size() > 0'>",
            "id IN",  // 如果列表不为空，生成 IN 查询
            "<foreach item='id' collection='list' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "<if test='list == null or list.size() == 0'>",
            "1 = 0",  // 如果列表为空，返回一个永远不匹配的条件
            "</if>",
            "</script>"
    })
    List<Recipes> findRecipesByIds(List<Integer> recipeIds);

    // 获取用户的健康状况数据（如：高血压、糖尿病等）
    @Select("SELECT health_status FROM users WHERE user_id = #{userId}")
    String findHealthStatusByUserId(int userId);

    // 获取符合特定营养需求范围的食谱
    @Select({
            "<script>",
            "SELECT * FROM recipes WHERE",
            "<if test='minCalories != null'> calories &gt;= #{minCalories}</if>",
            "<if test='maxCalories != null'> AND calories &lt;= #{maxCalories}</if>",
            "<if test='minProtein != null'> AND protein &gt;= #{minProtein}</if>",
            "<if test='maxProtein != null'> AND protein &lt;= #{maxProtein}</if>",
            "<if test='minFat != null'> AND fat &gt;= #{minFat}</if>",
            "<if test='maxFat != null'> AND fat &lt;= #{maxFat}</if>",
            "<if test='minCarb != null'> AND carbohydrate &gt;= #{minCarb}</if>",
            "<if test='maxCarb != null'> AND carbohydrate &lt;= #{maxCarb}</if>",
            "</script>"
    })
    List<Recipes> findRecipesByNutritionRange(@Param("minCalories") Integer minCalories,
                                              @Param("maxCalories") Integer maxCalories,
                                              @Param("minProtein") Integer minProtein,
                                              @Param("maxProtein") Integer maxProtein,
                                              @Param("minFat") Integer minFat,
                                              @Param("maxFat") Integer maxFat,
                                              @Param("minCarb") Integer minCarb,
                                              @Param("maxCarb") Integer maxCarb);



    // 随机获取食谱（用于未点赞未收藏的用户）
    @Select("SELECT * FROM recipes ORDER BY RAND() LIMIT 10")
    List<Recipes> findRandomRecipes();

    /**
     * **根据用户输入的部分营养数值进行筛选**
     * - 只筛选用户输入的数值
     * - 如果用户未输入某项，则使用 `Integer.MAX_VALUE` 作为最大值，确保不过滤该项
     */
    @Select("SELECT * FROM recipes WHERE " +
            "calories <= #{maxCalories} AND " +
            "protein <= #{maxProtein} AND " +
            "fat <= #{maxFat} AND " +
            "carbohydrate <= #{maxCarbohydrate}")
    List<Recipes> findRecipesByDynamicNutrition(int maxCalories, int maxProtein, int maxFat, int maxCarbohydrate);


}
